/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.base.bean;

import com.ibatis.dao.client.DaoManager;
import com.yunhoo.base.model.Economic;
import com.yunhoo.sys.db.config.DaoConfig;
import com.yunhoo.base.db.dao.EconomicDao;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.ToolKit;
import com.yunhoo.sys.util.tree.TreeRule;
import com.yunhoo.sys.util.tree.TreeUtil;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.richfaces.event.TreeSelectionChangeEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;

@ManagedBean(name = "economicBean")
@SessionScoped
public class EconomicBean implements Serializable {

    private static DaoManager daom;
    private static EconomicDao dao;
    private static String[] rule = TreeRule.getEconomicTreeRule();
    //
    private Economic node = null;
    private TreeNode rootNode = null;
    private boolean addOrUpdate = false;
    private boolean editStatus = false;

    public EconomicBean() {
        daom = DaoConfig.getDaoManager();
        dao = (EconomicDao) daom.getDao(EconomicDao.class);
    }

    public void initStatus() {
        editStatus = false;
        addOrUpdate = false;
    }

    private void loadTree() {
        try {
            rootNode = new TreeNodeImpl();
            List<Economic> list = dao.queryAll(new Economic(true));
            TreeUtil.addNode(list, rootNode, rule);
        } catch (SQLException ex) {
            Logger.getLogger(EconomicBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public TreeNode getRootNode() {
        if (this.rootNode == null) {
            loadTree();
        }
        return rootNode;
    }

    public void selected(TreeSelectionChangeEvent event) {
        Economic temp = (Economic) TreeUtil.listener(event);
        if (node == null) {
            node = new Economic(true);
        }
        node.setCode(temp.getCode());
        node.setName(temp.getName());
        node.setGrade(temp.getGrade());
        node.setIsEnd(temp.getIsEnd());
        initStatus();
    }

    public void add() {
        editStatus = true;
        addOrUpdate = true;
        node = new Economic(true);
    }

    public void update() {
        editStatus = true;
        addOrUpdate = false;
    }

    public void delete() {
        node.setSupCode(TreeUtil.parentCode(node.getCode(), rule));
        if (node.getIsEnd() == 0) {
            ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "非末级节点,不可删除!");
            return;
        }
        boolean ok = true;
        try {
            daom.startTransaction();
            List list = dao.directSubNodes(node);
            if (list != null && list.size() == 1) { //要删除节点,如果无兄弟节点,修改其父节点 isEnd属性为"1"
                Economic dp = new Economic(true);
                dp.setCode(node.getSupCode());
                dp.setIsEnd(1);
                dao.updateIsEnd(dp);
            }
            dao.delete(node);
            daom.commitTransaction();
        } catch (SQLException ex) {
            ok = false;
            daom.endTransaction();
            Logger.getLogger(DepartmentBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (ok) {
            node = null;
            loadTree();
            initStatus();
        }
    }

    public void checkSave() {
        boolean ok = true;
        try {
            daom.startTransaction();
            save();
            daom.commitTransaction();
        } catch (SQLException ex) {
            ok = false;
            daom.endTransaction();
            Logger.getLogger(CategoryBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (ok) {
            initStatus();
            loadTree();
        }
    }

    public void save() throws SQLException {
        node.setSupCode(TreeUtil.parentCode(node.getCode(), rule));
        node.setGrade(TreeUtil.codeGrade(node.getCode(), rule));
        if (addOrUpdate) {
            node.setJl_RQ(new Date());
            node.setJlr_ID(LoginParam.getPer().getCpsn_number());
            FacesContext context = FacesContext.getCurrentInstance();
            if (addOrUpdate && !TreeUtil.checkCodeLegal(node.getCode(), rule)) {
                ToolKit.msgs_info(context, null, "编码不符合规范!");
                return;
            }
            Economic codeRepeat = dao.queryById(node);
            if (codeRepeat != null) {
                ToolKit.msgs_info(context, null, "编码重复!");
                return;
            }
            Economic nameRepeat = dao.nameRepeatForInsert(node);
            if (nameRepeat != null) {
                ToolKit.msgs_info(context, null, "名称重复!");
                return;
            }

            //根节点 保存后返回
            if (node.getGrade() == 1) {
                node.setIsEnd(1);
                dao.insert(node);
                return;
            }
            Economic param = new Economic(true);
            param.setCode(node.getSupCode());
            Economic parent = dao.queryById(param);
            if (parent == null) {
                ToolKit.msgs_info(context, null, "上级不存在!");
                return;
            }
            if (parent.getIsEnd() == 1) { //父节点IsEnd是1的改为0;
                parent.setIsEnd(0);
                dao.updateIsEnd(parent);
            }
            node.setIsEnd(1);
            dao.insert(node);
        } else {
            node.setXg_RQ(new Date());
            node.setXgr_ID(LoginParam.getPer().getCpsn_number());
            Economic nameRepeat = dao.nameRepeatForUpdate(node);
            if (nameRepeat != null) {
                ToolKit.msgs_info(FacesContext.getCurrentInstance(), null, "名称重复!");
                return;
            }
            dao.update(node);
        }
    }

    public void cancel() {
        if (addOrUpdate) {
            node = null;
        }
        initStatus();
    }

    public Economic getNode() {
        return node;
    }

    public void setNode(Economic node) {
        this.node = node;
    }

    public boolean isAddOrUpdate() {
        return addOrUpdate;
    }

    public void setAddOrUpdate(boolean addOrUpdate) {
        this.addOrUpdate = addOrUpdate;
    }

    public boolean isEditStatus() {
        return editStatus;
    }

    public void setEditStatus(boolean editStatus) {
        this.editStatus = editStatus;
    }
}
